package top.zlhy7.controller;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author renyong
 * @date 2022/5/30 14:18
 * @description 测试redisson
 */
@RestController
@RequestMapping("redisson")
public class RedissonController {
    /**
     * redisson客户端
     */
    @Autowired
    private RedissonClient redissonClient;

    @GetMapping("test1/{lockKey}")
    public String test1(@PathVariable String lockKey){
        RLock lock = redissonClient.getLock(lockKey);
        try {
            lock.lock();
            Thread.sleep(10000);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
        return "已解锁";
    }
}
